Skip to content

Preserve empty message contexts in PO and MO output - #1373

Open
fhgffy wants to merge 1 commit into
python-babel:masterfrom
fhgffy:fix/empty-message-context
Open

fhgffy wants to merge 1 commit into
python-babel:masterfrom
fhgffy:fix/empty-message-context

Conversation

@fhgffy

@fhgffy fhgffy commented Oct 9, 2026

Copy link
Copy Markdown

GNU gettext distinguishes an empty context from an absent msgctxt, and Catalog stores both entries separately. The PO and MO writers currently omit an empty context, so messages sharing an ID are merged or lost when the output is read. Check whether context is None in the regular singular/plural serialization branches. Add PO/MO roundtrips for absent, empty, and named contexts, and verify translated lookup results.

Validation: all three new regressions fail before the change; 41 PO/MO tests and the complete suite (7,831 passed, 10 skipped, 2 xfailed) pass afterward. Project Ruff checks, whitespace/debug hooks, and sdist/wheel builds passed. Tests use the Makefile's python -X utf8 flag and the project's imported CLDR 48.2 data.

Specification: https://www.gnu.org/software/gettext/manual/html_node/Entries-with-Context.html

@akx akx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is good, but please clean up the comments -- 2026-10-10 doesn't mean much especially since it's already 2026-10-26 as I'm reviewing this...

Comment thread babel/messages/mofile.py
msgid = message.id.encode(catalog.charset)
msgstr = message.string.encode(catalog.charset)
if message.context:
# 2026-10-10: An empty context differs from an absent context.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 2026-10-10: An empty context differs from an absent context.
# An empty context differs from an absent context.

Comment thread babel/messages/pofile.py
Comment on lines +685 to +686
# 2026-10-10: Preserve explicitly empty contexts.
if message.context is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 2026-10-10: Preserve explicitly empty contexts.
if message.context is not None:
if message.context is not None: # Preserve explicitly empty contexts.

Comment thread babel/messages/pofile.py
Comment on lines +698 to +699
# 2026-10-10: Preserve explicitly empty contexts.
if message.context is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 2026-10-10: Preserve explicitly empty contexts.
if message.context is not None:
if message.context is not None: # Preserve explicitly empty contexts.



def test_empty_message_context():
# 2026-10-10: Empty contexts must remain distinct from absent contexts.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 2026-10-10: Empty contexts must remain distinct from absent contexts.
"""
Test that empty contexts remain distinct from absent contexts.
See https://www.gnu.org/software/gettext/manual/html_node/Entries-with-Context.html:
> Note that an empty context string and an absent msgctxt line do not mean the same thing.
"""

assert output["foo"].string in ((''), ('', ''))


# 2026-10-10: PO roundtrips must preserve empty singular and plural contexts.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 2026-10-10: PO roundtrips must preserve empty singular and plural contexts.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants